# Maintainer: Felix Yan <felixonmars@archlinux.org>
# Contributor  Bartłomiej Piotrowski <bpiotrowski@archlinux.org>
# Contributor: Thomas Dziedzic < gostrc at gmail >
# Contributor: James Campos <james.r.campos@gmail.com>
# Contributor: BlackEagle < ike DOT devolder AT gmail DOT com >
# Contributor: Dongsheng Cai <dongsheng at moodle dot com>
# Contributor: Masutu Subric <masutu.arch at googlemail dot com>
# Contributor: TIanyi Cui <tianyicui@gmail.com>
# Contributor: Jason Plum <jplum@archlinuxarm.org>

pkgname=nodejs
pkgver=4.1.1
pkgrel=1
pkgdesc='Evented I/O for V8 javascript'
arch=('i686' 'x86_64')
url='http://nodejs.org/'
license=('MIT')
provides=('nodejs-node-gyp')
depends=('openssl' 'zlib' 'icu') # 'libuv' 'v8' 'c-ares')
makedepends=('python2' 'procps-ng')
optdepends=('npm: nodejs package manager')
source=("http://nodejs.org/dist/v$pkgver/node-v$pkgver.tar.gz"
        '0001-arm-fixes.patch')
sha256sums=('6a610935ff52de713cf2af6a26002322e24fd7933a444436f0817a2b84e15a58'
            '13f14499c0a83d282ac07d1116a2d9196b4ae37dbd6914865f0b6549158789c4')

set_flags_for_arm() {
  if [ "$CARCH" == "arm" ]; then
    # CXXFLAGS is here to bash it over the head. It has a bad habit.
    CONFIG="--with-arm-float-abi=soft"
    CXXFLAGS="$CXXFLAGS -march=armv5t -mno-unaligned-access" ;
    GYPFLAGS="-Darm_thumb -Darm_float_abi=soft -Darm_version=5 -Darm_fpu= -Darm_test_noprobe=on"
  fi
  if [ "$CARCH" == "armv6h" ]; then
    CONFIG="--with-arm-float-abi=hard"
    GYPFLAGS="-Darm_thumb -Darm_float_abi=hard -Darm_version=6 -Darm_fpu=vfpv2"
  fi
  if [ "$CARCH" == "armv7h" ]; then
    CONFIG="--with-arm-float-abi=hard"
    GYPFLAGS="-Darm_thumb -Darm_float_abi=hard -Darm_version=7 -Darm_fpu=vfpv3-d16"
  fi
  export CXXFLAGS
  export GYPFLAGS
}

prepare() {
  cd node-v$pkgver

  patch -p1 -i ../0001-arm-fixes.patch

  msg 'Fixing for python2 name'
  find -type f -exec sed \
    -e 's_^#!/usr/bin/env python$_&2_' \
    -e 's_^\(#!/usr/bin/python2\).[45]$_\1_' \
    -e 's_^#!/usr/bin/python$_&2_' \
    -e 's_^\( *exec \+\)python\( \+.*\)$_\1python2\2_'\
    -e 's_^\(.*\)python\( \+-c \+.*\)$_\1python2\2_'\
    -e "s_'python'_'python2'_" -i {} \;
  find test/ -type f -exec sed 's_python _python2 _' -i {} \;
}

build() {
  cd node-v$pkgver

  export PYTHON=python2

  set_flags_for_arm

  # short-cutting python=python2 for GYP, as it will only be used here.
  export PYTHON=python2
  if [ ! -f "${srcdir}/python" ]; then
    ln -s `which python2` ${srcdir}/python ;
  fi
  PATH="${srcdir}:$PATH";
  export PATH ;

# we're going to attept to make use of this example
# https://github.com/joyent/node/issues/2131#issuecomment-3208846
# https://gist.github.com/1574158   

  ./configure \
    --prefix=/usr \
    --with-intl=system-icu \
    --without-npm \
    --without-snapshot \
    --shared-openssl \
    --shared-zlib \
    ${CONFIG}
#    --shared-libuv
#    --shared-v8 \
#    --shared-v8-libpath=/usr/lib \
#    --shared-v8-includes=/usr/include 

  [ "$CARCH" == "arm" ] && sed -i 's/vfpv3//' config.gypi
  [ "$CARCH" == "armv6h" ] && sed -i 's/vfpv3/vfp/' config.gypi

  make
}

check() {
  cd node-v${pkgver}

  make test || warning "Tests failed"
}

package() {
  cd node-v${pkgver}

  # suppress re-compile(with out customized flags) by 'make install'.
  set_flags_for_arm

  make DESTDIR=${pkgdir} install

  # install docs as per user request
  install -d ${pkgdir}/usr/share/doc/nodejs
  cp -r doc/api/{*.html,assets} \
    ${pkgdir}/usr/share/doc/nodejs

  install -D -m644 LICENSE \
    ${pkgdir}/usr/share/licenses/nodejs/LICENSE
}

# vim:set ts=2 sw=2 et:
